<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Requests\LoveUserPostRequest;

class UserCareController extends Controller
{
    
    public function getIndex()
    {
       //初始化操作

      //获取全部以及分类
       $type = DB::table('type')->where('pid',0)->get();
      // dd($type);

      //获取配置信息
       $config = DB::table('config')->first();

      //友情链接
       $blinks = DB::table('blink')->get();

      //遍历下部的栏目
      $datas = self::pushCates(self::pushType(),0);

      //查询线下所有的钱数
      $money = DB::table('loveuser')->get();
      $moneys = '';
      foreach($money as $k=>$v)
      {
        $moneys += $v->account;

      }

      //查询最近的8条
      $nums = DB::table('loveuser')->paginate(8);
      
    

      // var_dump($type);
       return view('Home.loveser.care',['type'=>$type,'config'=>$config,'blinks'=>$blinks,'datas'=>$datas,'moneys'=>$moneys,'nums'=>$nums]);
    }

     //获取全部一级分类板块
    public static function pushType()
    {
        return $cates = DB::table('type')->get();
    }

    
    //获取二级分类
    public  static function pushCates($cates,$pid)
    {
        $data = [];
        foreach($cates as $k=>$v)
        {
            if($v->pid == $pid){

               $v->sub = self::pushCates($cates,$v->id);
               $data[] = $v;
            }
        }
        return $data;
    }


    //线上捐款表单
    public function getCare()
    {
        // echo 22;
        // 初始化

    //获取全部以及分类
       $type = DB::table('type')->where('pid',0)->get();
      // dd($type);

      //获取配置信息
       $config = DB::table('config')->first();

      //友情链接
       $blinks = DB::table('blink')->get();

      //遍历下部的栏目
      $datas = self::pushCates(self::pushType(),0);

      //

      // var_dump($type);
       return view('Home.loveser.userpost',['type'=>$type,'config'=>$config,'blinks'=>$blinks,'datas'=>$datas]);


    }

    //网上捐款添加
    public function postInsert(LoveUserPostRequest $request)
    {
      dd();
        //提取数据
      $data = $request->except('_token');

      $data['status'] = 1;
      $data['time']=time();

      //入库操作 
      $res = DB::table('loveuser')->insert($data);

      if($res){

        return redirect('/homes/care/care')->with('success','捐款成功');
      }else{

        return back()->with('error','捐款失败');
      }
    }
   
}
